package tomyang.app.facecheck.recyclerview;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * @author Tom Yang
 * @date 2018/02/02
 */

public abstract class StickinessHeaderItemDecoration extends RecyclerView.ItemDecoration {

    int headerHeight = 120;

    Paint bgPaint = new Paint();
    Paint paint = new Paint();
    {
        bgPaint.setColor(Color.WHITE);
        bgPaint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.RED);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(45);
    }

    public StickinessHeaderItemDecoration setHeaderHeight(int height) {
        headerHeight = height;
        return this;
    }

    public StickinessHeaderItemDecoration setHeaderTextColor(int colorValue) {
        paint.setColor(colorValue);
        return this;
    }

    public StickinessHeaderItemDecoration setHeaderBackgroundColor(int colorValue) {
        bgPaint.setColor(colorValue);
        return this;
    }

    public StickinessHeaderItemDecoration setHeaderTextSize(int size) {
        paint.setTextSize(size);
        return this;
    }

    public StickinessHeaderItemDecoration setHeaderTextAlign(Paint.Align align) {
        paint.setTextAlign(align);
        return this;
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int count = parent.getChildCount();
        View topView = null;//当前显示的List中的first view
        int topViewHeader = -1;//top view的header index
        int topViewIndex = -1;//top view在adapter中的index
        //遍历List，在需要显示header的地方绘制出header
        for (int i = 0 ; i < count ; i ++) {
            View child = parent.getChildAt(i);
            //找到对应view 的index
            int index = parent.getChildAdapterPosition(child);
            //根据index找到对应的粘性头部的index
            int header = getIndexHeaderTag(index);
            int preHeader = -1;
            if(index - 1 >= 0) {//找出该位置的上一个item是否的头部index
                preHeader = getPreHolderHeader(index);
            }
            if(topView == null) {
                topView = child;
                topViewHeader = header;
                topViewIndex = index;
            } else {
                if(child.getTop() < topView.getTop() ) {
                    topView = child;
                    topViewHeader = header;
                    topViewIndex = index;
                }
            }
            if(preHeader != header) {//当前item与上一个item不同属一个header,需要画出其对应的header
                drawHeader(c, child ,header);
            }
        }
        //在最顶端绘制出top view的头部
        if(topView != null) {
            int top = parent.getPaddingTop();
            int bottom = top + headerHeight;
            //可绘制范围
            Rect rect = new Rect(topView.getLeft(),top,topView.getRight() , bottom);
            //找出下一个item的header
            int nextHeader = getIndexHeaderTag(topViewIndex + 1);
            if(topViewHeader != nextHeader) {//入下一个item的header不同属一个header，需要计算first view的偏移值，header碰撞
                RecyclerView.ViewHolder viewHolder = parent.findViewHolderForAdapterPosition(topViewIndex + 1);
                if(viewHolder != null) {
                    //偏移值，计算next item的header位置 nextHeaderTop=(viewHolder.itemView.getTop() - headerHeight)
                    //top view 往上的偏移量 headerHeight - nextHeaderTop
                    int offset = headerHeight - (viewHolder.itemView.getTop() - headerHeight);
                    if(offset > 0) {//正数表示上偏，负数是两者之间有间隙，不需要进行header偏移
                        rect.offset(0,-offset);
                    }
                }
            }
            realDrawHeader(c , getHeaderText(topViewHeader) , rect);
        }
    }

    private void drawHeader( Canvas canvas , View view , int header) {
        int top = view.getTop();
        Rect rect = new Rect(view.getLeft(),top - headerHeight , view.getRight(), top);
        String str = getHeaderText(header);
        realDrawHeader(canvas , str , rect);
    }
    /**默认的header绘制方案*/
    private void realDrawHeader(Canvas canvas , String header , Rect rect) {
        if(!drawHeader(canvas,header,rect)) {
            canvas.drawRect(rect , bgPaint);
            canvas.drawText(header,rect.left , rect.centerY(), paint);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int index = parent.getChildAdapterPosition(view);
        int header = getIndexHeaderTag(index);
        int preHeader = -1;
        if(index - 1 >= 0) {
            preHeader = getPreHolderHeader(index);
        }
        if(preHeader != header) {
            outRect.top = headerHeight;
        } else {
            outRect.top = getIndexDivider(index);
        }
    }

    public int getPreHolderHeader(int index ) {
        return getIndexHeaderTag(index-1);
    }

    /**
     * current index header tag
     * @param index
     * @return
     */
    public abstract int getIndexHeaderTag(int index);

    /**
     * header content
     * @param header
     * @return
     */
    public abstract String getHeaderText(int header );

    /**
     * draw header content
     * @param c
     * @param headerText
     * @param rect
     * @return
     */
    public abstract boolean drawHeader(Canvas c , String headerText , Rect rect);

    /**
     * divider height
     * @param index
     * @return
     */
    public abstract int getIndexDivider(int index);

}
